package com.admin.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.admin.bean.CompanyBean;
import com.admin.service.CompanyService;
import com.admin.util.BaseUtil;
import com.admin.util.PageBean;

@Controller
@RequestMapping(value="/company")
public class CompanyAction {

	private static final String LIST 	= "companyList";
	private static final String VIEW 	= "companyView";
	
	@Autowired
	private CompanyService companyService;
	
	@RequestMapping()
	public String showList(HttpServletRequest request){
		
		String spname = request.getParameter("spname");
		String bussiness = request.getParameter("bussiness");
		
		StringBuilder condition = new StringBuilder(" 1=1 ");
		if(spname!=null&&spname.length()>0) {
			condition.append(" and companyName like '%");
			condition.append(spname);
			condition.append("%' ");
		}
		
		if(bussiness!=null&&bussiness.length()>0) {
			condition.append(" and bussiness like '%");
			condition.append(bussiness);
			condition.append("%' ");
		}
		
		int totalCount = companyService.findCount(condition.toString());
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<CompanyBean> pageBean = new PageBean<CompanyBean>(totalCount,currentPage);
		List<CompanyBean> companyList = companyService.findList(condition.toString(),pageBean.getStart(), pageBean.getPageSize());
		pageBean.setResult(companyList);
		pageBean.setUrl("company?1=1");
		request.setAttribute("pageBean", pageBean);
		return LIST;
	}
	
	@RequestMapping(value="/add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response) throws IOException{
		CompanyBean company = new CompanyBean();
		company.setCompanyName(request.getParameter("companyName"));
		company.setCompanyMemo(request.getParameter("companyMemo"));
		company.setCompanyAddr(request.getParameter("companyAddr"));
		company.setCompanyTel(request.getParameter("companyTel"));
		company.setCompanyFax(request.getParameter("companyFax"));
		company.setCompanyHome(request.getParameter("companyHome"));
		company.setBussiness(request.getParameter("bussiness"));
		company.setSppercent(request.getParameter("percent"));
		company.setSpcheck(request.getParameter("check"));
		company.setCommandMemo(request.getParameter("commandMemo"));
		company.setSpstatus(BaseUtil.stringToInt(request.getParameter("status")));
		
		int flag = companyService.insertCompany(company);
		response.setContentType("text/html");
		response.getWriter().print(flag);
		return null;
	}
	
	@RequestMapping(value="/view/{companyId}",method=RequestMethod.GET)
	public String view(@PathVariable int companyId,HttpServletRequest request){
		CompanyBean company = companyService.findCompanyById(companyId);
		System.out.println(company.getSpstatus());
		request.setAttribute("company", company);
		request.setAttribute("spstatus", company.getSpstatus());
		return VIEW;
	}
	
	
	@RequestMapping("/modify")
	public ModelAndView modify(HttpServletRequest request,HttpServletResponse response) throws IOException{
		CompanyBean company = new CompanyBean();
		int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
		company.setCompanyId(companyId);
		company.setCompanyName(request.getParameter("companyName"));
		company.setCompanyMemo(request.getParameter("companyMemo"));
		company.setCompanyAddr(request.getParameter("companyAddr"));
		company.setCompanyTel(request.getParameter("companyTel"));
		company.setCompanyFax(request.getParameter("companyFax"));
		company.setCompanyHome(request.getParameter("companyHome"));
		company.setBussiness(request.getParameter("bussiness"));
		company.setSppercent(request.getParameter("percent"));
		company.setSpcheck(request.getParameter("check"));
		company.setCommandMemo(request.getParameter("commandMemo"));
		company.setSpstatus(BaseUtil.stringToInt(request.getParameter("status")));
		int flag = companyService.updateCompany(company);
		response.setContentType("text/html");
		response.getWriter().print(flag);
		return null;
	}
	
	@RequestMapping("/del/{companyId}")
	public ModelAndView del(@PathVariable int companyId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		int flag = companyService.delCompany(companyId);
		response.setContentType("text/html");
		response.getWriter().print(flag);
		return null;
	}
}
